﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;

namespace Plumber.Sales.ModuleData
{
    internal class RadarQuotationsParam : RadarParameters
    {
        public RadarQuotationsParam(string quotationNo)
        {
            Params = new List<object>();
            Params.Add(quotationNo);
        }
    }

    internal class RadarSaleInvoicesParam : RadarParameters
    {
        public RadarSaleInvoicesParam(string invoiceNo)
        {
            Params = new List<object>();
            Params.Add(invoiceNo);
        }
    }

    internal class RadarTemplatesParam : RadarParameters
    {
        public RadarTemplatesParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    internal class RadarSentencesParam : RadarParameters
    {
        public RadarSentencesParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    #region Radar RadarSaleInvoices

    internal class RadarSaleInvoices : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1, p2, pFrom, pTo, pType;
        private InvoiceType invType;

        public RadarSaleInvoices(InvoiceType invtype)
            : base()
        {
            invType = invtype;
            rdrCodeColumn = PL_SalesInvoices.InvoiceNo.Name;
            rdrDescColumn = PL_SalesInvoices.Description.Name;
            rdrNameSpace = "Plumber.Plumber.Sales.Forms.SalesInvoice";
            this.ResourceManager = Properties.Resources.ResourceManager;
        }

        protected override Table GetRadarTable()
        {
            return new PL_SalesInvoices();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", PL_SalesInvoices.InvoiceNo);
            p2 = new SqlABParameter("@p2", PL_SalesInvoices.InvoiceType);

            qb.Clear();
            qb.AddSelectAll(PL_SalesInvoices.Name);
            qb.AddFrom(PL_SalesInvoices.Name);
            qb.AddCompare(PL_SalesInvoices.InvoiceNo, p1);
            qb.AddCompare(PL_SalesInvoices.InvoiceType, p2);
            qb.AddOrder(PL_SalesInvoices.InvoiceNo);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);
            sqlCmd.Parameters.Add(p2);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
            p2.Value = invType.Int();
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_SalesInvoices.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            qb.Clear();
            qb.AddSelectAll(PL_SalesInvoices.Name);
            qb.AddFrom(PL_SalesInvoices.Name);
            pFrom = new SqlABParameter("@p2", PL_SalesInvoices.ReleaseDate);
            pTo = new SqlABParameter("@p3", PL_SalesInvoices.ReleaseDate);
            pType = new SqlABParameter("@p4", PL_SalesInvoices.InvoiceType);
            qb.AddCompare(PL_SalesInvoices.InvoiceType, pType);

            qb.AddManualFilter("({0} BETWEEN {1} AND {2} OR {3} = '0')",
                PL_SalesInvoices.ReleaseDate, pFrom, pTo, PL_SalesInvoices.SalePayed);

            pFrom.Value = GlobalInfo.CurrentDate.StartYear();
            pTo.Value = GlobalInfo.CurrentDate.EndYear();

            pType.Value = invType.Int();

            sqlCmd.Parameters.Add(pFrom);
            sqlCmd.Parameters.Add(pTo);
            sqlCmd.Parameters.Add(pType);

            qb.AddFilter(findQuery);
            qb.AddOrder(string.Format("YEAR({0})", PL_SalesInvoices.ReleaseDate.Name));
            qb.AddOrder(PL_SalesInvoices.InvoiceNo);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            string InvoiceNo = DataGridViewRowValues.GetValue<string>(row, PL_SalesInvoices.InvoiceNo);
            return new RadarSaleInvoicesParam(InvoiceNo);
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarSaleInvoicesParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion

    #region Radar Sentences

    internal class RadarSentences : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;
        private SqlABParameter p2;

        public RadarSentences()
            : base()
        {
            rdrCodeColumn = PL_Sentences.ID.Name;
            rdrDescColumn = PL_Sentences.Title.Name;
            rdrNameSpace = "Plumber.Plumber.Sales.Forms.Sentences";
        }

        protected override Table GetRadarTable()
        {
            return new PL_Sentences();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", PL_Sentences.ID);

            qb.Clear();
            qb.AddSelectAll(PL_Sentences.Name);
            qb.AddFrom(PL_Sentences.Name);
            qb.AddCompare(PL_Sentences.ID, p1);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_Sentences.Text.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p2 = new SqlABParameter("@p2", PL_Sentences.ID);
            qb.Clear();
            qb.AddSelectAll(PL_Sentences.Name);
            qb.AddFrom(PL_Sentences.Name);
            qb.AddFilter(findQuery);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarSentencesParam(DataGridViewRowValues.GetValue<string>(row, PL_Sentences.ID));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarSentencesParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion

    #region Radar Templates

    internal class RadarTemplates : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1,p2,p3;
        TemplateType TempType = TemplateType.E_Sale;

        public RadarTemplates(TemplateType temptype)
            : base()
        {
            TempType = temptype;
            rdrCodeColumn = PL_Templates.Code.Name;
            rdrDescColumn = "";
            rdrNameSpace = "Plumber.Plumber.Sales.Forms.Templates";
        }

        protected override Table GetRadarTable()
        {
            return new PL_Templates();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", PL_Templates.Type);
            p2 = new SqlABParameter("@p2", PL_Templates.Code);

            qb.Clear();
            qb.AddSelectAll(PL_Templates.Name);
            qb.AddFrom(PL_Templates.Name);
            qb.AddCompare(PL_Templates.Type, p1);
            qb.AddCompare(PL_Templates.Code, p2);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);
            sqlCmd.Parameters.Add(p2);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
            p2.Value = param[1];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_Templates.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p3 = new SqlABParameter("@p3", PL_Templates.Type);
            p3.Value = TempType;
            sqlCmd.Parameters.Add(p3);

            qb.Clear();
            qb.AddSelectAll(PL_Templates.Name);
            qb.AddFrom(PL_Templates.Name);
            qb.AddCompare(PL_Templates.Type, p3);
            qb.AddFilter(findQuery);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p2.Value = PL_Templates.Code.DefaultValue;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarTemplatesParam(DataGridViewRowValues.GetValue<string>(row, PL_Templates.Code));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarTemplatesParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion

    #region Radar RadarQuotations

    internal class RadarQuotations : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;

        public RadarQuotations()
            : base()
        {
            rdrCodeColumn = PL_Quotations.QuotationNo.Name;
            rdrDescColumn = PL_Quotations.Description.Name;
            rdrNameSpace = "Plumber.Plumber.Sales.Forms.Quotations";
        }

        protected override Table GetRadarTable()
        {
            return new PL_Quotations();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", PL_Quotations.QuotationNo);

            qb.Clear();
            qb.AddSelectAll(PL_Quotations.Name);
            qb.AddFrom(PL_Quotations.Name);
            qb.AddCompare(PL_Quotations.QuotationNo, p1);
            qb.AddOrder(PL_Quotations.QuotationNo);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_Quotations.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            qb.Clear();
            qb.AddSelectAll(PL_Quotations.Name);
            qb.AddFrom(PL_Quotations.Name);

            qb.AddFilter(findQuery);
            qb.AddOrder(PL_Quotations.ReleaseDate);
            qb.AddOrder(PL_Quotations.QuotationNo);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            string quotationNo = DataGridViewRowValues.GetValue<string>(row, PL_Quotations.QuotationNo);
            return new RadarSaleInvoicesParam(quotationNo);
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarQuotationsParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion
}